/* pages/login/login.js */
// 获取应用实例
const app = getApp()
const db=wx.cloud.database().collection('login')
Page({
  data: {
    stu:'',
    pwd:'',
    tip:false,
    tip_content:''
  },
  // 事件处理函数
  login() {
    const that=this
    if(that.data.stu&&that.data.pwd){
      db.where({
        stuno:that.data.stu
      }).get({
        success(res){
          console.log(res)
          if(res.data.length>0){
            if(that.data.pwd==res.data[0].pwd){
              wx.setStorageSync('user', res.data[0].stuno)
              if(res.data[0].role=='stu'){
                wx.setStorageSync('role', 'stu') 
                wx.setStorageSync('name', res.data[0].name)
                wx.setStorageSync('grade', res.data[0].grade)
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }else if(res.data[0].role=='admin'){
                wx.setStorageSync('role', 'adm') 
                wx.switchTab({
                  url: '/pages/admin/admin?major='+res.data[0].major,
                })
              }
             
              
            }else that.tip("密码错误！")
          }
          else {
           that.tip("账号不存在！")
          }
        }
      }) 
    }else that.tip("账号或密码不为空！")
   
   
  },
  stu_in:function(e){
    this.data.stu=e.detail.value
  },
  pwd_in:function(e){
    this.data.pwd=e.detail.value
  },
  tip:function (str){
    const that=this
    
    that.setData({
      tip_content:str,
      tip:true
    })
    setTimeout(function(){
      that.setData({
        tip:false
      })
    },1000)
  },
 

  onLoad() {
       
  },

})
